package com.example.dell.test1;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.util.Log;
import android.view.View;
import android.view.Window;
import android.widget.Button;
import android.widget.EditText;
import android.widget.Toast;

/**
 * Created by klren on 2016/5/31.
 */
public class MainActivity extends Activity {
    Button loginBtn;
    Button zhuceBtn;
    EditText name;
    EditText pwd;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        //隐藏标题栏
        //requestWindowFeature(Window.FEATURE_NO_TITLE);
        btnThings();

    }

    private void btnThings(){
        loginBtn= (Button)findViewById(R.id.dlBtn);
        zhuceBtn = (Button)findViewById(R.id.zcBtn);
        name = (EditText)findViewById(R.id.name);
        pwd = (EditText)findViewById(R.id.pwd);

        /**
         * 登录按钮点击事件
         */
        loginBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                String username = name.getText().toString();
                String password = pwd.getText().toString();
                Log.i("test",username+" "+password);
                UserService US = new UserService(MainActivity.this);
                boolean flag = US.login(username,password);

                /**
                 * 判断如果flag为1 ，则登录成功，跳转到首页,并发送登陆成功广播;反之提示登录失败
                 */
                if (flag){
                    Intent intent = new Intent(MainActivity.this,HomeActivity.class);
                    Intent intent1 = new Intent("broadcast");
                    sendBroadcast(intent1);
                    startActivity(intent);
                }else{
                    Toast.makeText(MainActivity.this,"登录失败",Toast.LENGTH_LONG).show();
                }

            }
        });

        /**
         * 注册按钮点击事件，跳转到注册页面
         *
         */
        zhuceBtn.setOnClickListener(new View.OnClickListener(){
            @Override
            public void onClick(View v) {
                Intent intent = new Intent(MainActivity.this,zhuceActivity.class);
                startActivity(intent);
            }
        });
    }
}
